﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;

namespace SheepCommon.API.SheepCommands
{
    public class SheepArchivesQueryHandler : IRequestHandler<SheepArchivesQueryCommand, ResultPage<SheepArchivesQueryDto>>
    {

        private readonly IBaseRepository<SheepFarm> _sheepFarmsRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieIdRepository;
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;//羊档案
        private readonly IMapper _mapper;

        public SheepArchivesQueryHandler(IBaseRepository<SheepFarm> sheepFarmsRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieIdRepository, IBaseRepository<SheepArchives> sheepArchivesRepository, IMapper mapper)
        {
            _sheepFarmsRepository = sheepFarmsRepository;
            _cottageRepository = cottageRepository;
            _fieIdRepository = fieIdRepository;

            _sheepArchivesRepository = sheepArchivesRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 业务处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepArchivesQueryDto>> Handle(SheepArchivesQueryCommand request, CancellationToken cancellationToken)
        {

            var result = new ResultPage<SheepArchivesQueryDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            //List<SheepArchivesQueryDto> res = new List<SheepArchivesQueryDto>();
            try
            {
                var sheepArchives = await (from a in await _sheepArchivesRepository.GetAll()
                                           //羊档案联羊场
                                           join d in await _sheepFarmsRepository.GetAll() on a.FarmId equals d.FarmId
                                           join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                           join c in await _fieIdRepository.GetAll() on a.FieldId equals c.FieldId
                                        select new SheepArchivesQueryDto()
                                        {
                                            FarmId = a.FarmId,
                                            FarmFullName = d.FarmFullName,
                                            CottageId = b.CottageId,
                                            CottageName = b.CottageName,
                                            FieldId = c.FieldId,
                                            FieldName = c.FieldName,
                                            RecordId = a.RecordId,
                                            RecordAuricle = a.RecordAuricle,
                                            RecordCorpuscle = a.RecordCorpuscle,
                                            RecordMother = a.RecordMother,
                                            RecordFather = a.RecordFather,
                                            RecordBreed = a.RecordBreed,
                                            RecordBorn = a.RecordBorn,
                                            RecordDate = a.RecordDate,
                                            RecordSite = a.RecordSite,
                                            RecordWeight = a.RecordWeight,
                                            RecordRemark = a.RecordRemark,
                                            AdmissionDate = a.AdmissionDate,
                                            Sex = a.Sex,
                                            Gene = a.Gene,
                                            RecordType = a.RecordType,
                                            SheepImage = a.SheepImage,
                                            Stage = a.Stage,
                                            ProductionGrade = a.ProductionGrade,
                                            SalesGrade = a.SalesGrade,
                                            ReproductiveState = a.ReproductiveState,
                                            ReproductiveStateDays = a.ReproductiveStateDays,
                                            PresenceStatus = a.PresenceStatus,
                                            IsPregnancyTest = a.IsPregnancyTest,
                                            WeanWeight = a.WeanWeight,
                                            WeanDate = a.WeanDate,
                                            DaysOld = DateTime.Now.Day-a.RecordDate.Day,//日龄
                                            MonthsOld=0
                                        }).ToListAsync();
                //可视耳号
                if (!string.IsNullOrEmpty(request.RecordAuricle))
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordAuricle == request.RecordAuricle).ToList();
                }
                //电子耳号
                if (!string.IsNullOrEmpty(request.RecordCorpuscle))
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordCorpuscle == request.RecordCorpuscle).ToList();
                }
                //品种
                if (!string.IsNullOrEmpty(request.RecordBreed))
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordBreed == request.RecordBreed).ToList();
                }
                //性别
                if (!string.IsNullOrEmpty(request.Sex))
                {
                    sheepArchives = sheepArchives.Where(c => c.Sex == request.Sex).ToList();
                }
                //出生日期
                if (request.RecordDate != null)
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordDate == request.RecordDate).ToList();
                }
                //月龄
                if (request.AgeInMonths > 0)
                {
                    sheepArchives = sheepArchives.Where(c => c.MonthsOld == request.AgeInMonths).ToList();
                }
                //生长阶段
                if (request.Stage > 0)
                {
                    sheepArchives = sheepArchives.Where(c => c.Stage == request.Stage).ToList();
                }
                //栋舍
                if (request.CottageId > 0)
                {
                    sheepArchives = sheepArchives.Where(c => c.CottageId == request.CottageId).ToList();
                }
                //栏位
                if (request.FieldId > 0)
                {
                    sheepArchives = sheepArchives.Where(c => c.FieldId == request.FieldId).ToList();
                }
                //羊只类型 0种母 1种公 2后裔
                if (request.RecordType > -1)
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordType == request.RecordType).ToList();
                }
                //生产等级
                if (request.ProductionGrade>0)
                {
                    sheepArchives = sheepArchives.Where(c => c.ProductionGrade == request.ProductionGrade).ToList();
                }
                //基因等级
                if (!string.IsNullOrEmpty(request.Gene))
                {
                    sheepArchives = sheepArchives.Where(c => c.Gene == request.Gene).ToList();
                }
                //销售等级
                if (request.SalesGrade>0)
                {
                    sheepArchives = sheepArchives.Where(c => c.SalesGrade == request.SalesGrade).ToList();
                }
                //繁殖状态
                if (request.ReproductiveState> 0)
                {
                    sheepArchives = sheepArchives.Where(c => c.ReproductiveState == request.ReproductiveState).ToList();
                }
                //繁殖状态天数
                if (request.ReproductiveStateDays > 0)
                {
                    sheepArchives = sheepArchives.Where(c => c.ReproductiveStateDays == request.ReproductiveStateDays).ToList();
                }
                //母亲耳号
                if (!string.IsNullOrEmpty(request.RecordMother))
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordMother == request.RecordMother).ToList();
                }
                //父亲耳号
                if (!string.IsNullOrEmpty(request.RecordFather))
                {
                    sheepArchives = sheepArchives.Where(c => c.RecordFather == request.RecordFather).ToList();
                }
                //在场状态
                if (request.PresenceStatus>0)
                {
                    sheepArchives = sheepArchives.Where(c => c.PresenceStatus == request.PresenceStatus).ToList();
                }
                result.TotalCount = sheepArchives.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                sheepArchives = sheepArchives.OrderBy(c => c.RecordId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheepArchives;
            }
            catch (Exception)
            {

                throw;
            }
            return result;

        }
    }
}
